<?php

include_once ('bn_path.php');

class bn_files
{
    protected static $instance;

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function get_filename($filename)
    {
        return pathinfo($filename, PATHINFO_BASENAME);
    }

    public function get_fileEXT($filename)
    {
        return pathinfo($filename, PATHINFO_EXTENSION);
    }

    public function get_filedir($filename)
    {
        return pathinfo($filename, PATHINFO_DIRNAME);
    }

    public function get_filebasename($filename)
    {
        return pathinfo($filename, PATHINFO_FILENAME);
    }

    public function read_file($filepath, $flag = '')
    {
        if ($flag == '')
            $lines = file($filepath);
        else
            $lines = file($filepath, $flag);
        $content = '';
        foreach ($lines as $line)
        {
            $content .= "$line\n";
        }
        return $content;
    }


    public function read_fileArray($filepath, $count = 0)
    {
        $lines = file($filepath);
        if (count == 0)
            return $lines;
        else
            return array_slice($lines, 0, $count);
    }


    public function Write_file($filepath, $text)
    {
        file_put_contents($filepath, $text);
    }


    public function unzip_allfile($zipfile, $path)
    {
        $zip = new ZipArchive;
        if ($zip->open($zipfile) === true)
        {
            $zip->extractTo(bn_add_endpath_slash($path));
            $zip->close();
            return true;
        } else
        {
            return false;
        }
    }

    public function unzip_files($file, $path, $extfiles = array())
    {
        $zip = new ZipArchive;
        if ($zip->open($file) === true)
        {
            $zip->extractTo(bn_add_endpath_slash($path), $extfiles);
            $zip->close();
            return true;
        } else
        {
            return false;
        }
    }

    public function zip_filelist($zipfile)
    {
        $list = array();
        $zip = new ZipArchive;
        if ($zip->open($zipfile) === true)
        {
            for ($i = 0; $i <= $zip->numFiles; $i++)
            {
                $list[] = $zip->getNameIndex($i);
            }
            $zip->close();
        }
        return $list;
    }

    public function has_file_in_zipfile($zipfile, $filename)
    {
        $list = array();
        $zip = new ZipArchive;
        if ($zip->open($zipfile) === true)
        {
            for ($i = 0; $i <= $zip->numFiles; $i++)
            {
                if (strtolower($zip->getNameIndex($i)) == $filename)
                {
                    $zip->close();
                    return true;
                }
            }
            $zip->close();
        }
        return false;
    }

    public function zip_dir($dirpath, $zipFileName, $delete = false)
    {
        $zip = new ZipArchive();
        $dir = new bn_directory;
        if ($zip->open($zipFileName, ZIPARCHIVE::CREATE) === true)
        {
            $dirpath = bn_add_endpath_slash($dirpath);
            $list = $dir->get_allsubdir($dirpath);
            $zip->addemptydir(basename($dirpath));
            $basedir = dirname($dirpath) . '/';
            foreach ($list as $file)
            {
                $name = str_replace($basedir, '', $file);
                if (is_dir($file))
                    $zip->addemptydir($name);
                else
                    $zip->addFile($file, $name);
            }
            $zip->close();
            if ($delete)
            {
                arsort($list);
                foreach ($list as $file)
                {
                    if (is_dir($file))
                        rmdir($file);
                    else
                        unlink($file);
                }
                rmdir($dirpath);
            }
            unset($zip);
            unset($dir);
        }
    }

    public function zip_files($files = array(), $zipfilename, $delete = false)
    {
        if (!is_array($files))
            $files = explode(',', $files);
        $zip = new ZipArchive();
        if ($zip->open($zipfilename, ZIPARCHIVE::CREATE) === true)
        {
            foreach ($files as $file)
            {
                $name = basename($file);
                $zip->addFile($file, $name);
            }
            $zip->close();
            foreach ($files as $file)
            {
                unlink($file);
            }
        }
        unset($zip);
        unset($dir);
    }

}

?>